/**
 * Created by Eric.Dou on 4/14/14.
 */
define(function (require) {
    var comm = require('lib/commons');
    return function (helper, $) {
        helper.register('createSubComponts',
            function (options) {
                this.condition = function (obj) {
                    if (!comm.hasPro(obj, 'layout')) {
                        return false;
                    } else if (!comm.hasPro(obj, 'target')) {
                        return false;
                    }
                    return true;
                };

                var layout = options['layout'];
                var target = options['target'];

                var appendFn = 'appendTo'
                if (comm.hasPro(layout, 'belongWidgetType')) {
                    appendFn = layout['belongWidgetType'];
                }

                var belongTo = 'body';
                if (comm.hasPro(layout, 'belongTo')) {
                    belongTo = layout['belongTo'];
                }
                if ($.isFunction(belongTo)) {
                    belongTo($);
                }
                else if (null != belongTo && $.type(belongTo) == 'object') {
                    if (comm.hasPro(belongTo, 'method') && comm.hasPro(belongTo, 'options') && comm.hasPro(layout, 'belongTarget')) {
                        var method = belongTo['method'], opt = belongTo['options'], belongTarget = layout['belongTarget'];
                        $(belongTarget)[appendFn](method, opt);
                    }
                } else if ($.type(belongTo) == 'string') {
                    $(target)[appendFn](belongTo);
                }
                return {'createSubComponts': true};
            });
    }
});
